/*
 * Copyright (C) 2004, Velocityme Partnership
 * StateMachineActionForm.java
 *
 * Created on 19 October 2004, 06:26
 */

package com.velocityme.www.actionforms;

import com.velocityme.entity.Role;
import com.velocityme.entity.State;
import com.velocityme.entity.Status;
import com.velocityme.entity.Transition;
import com.velocityme.www.dto.IntegerString;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class StateMachineActionForm extends ActionForm {
    
    public class InternalTransition implements Serializable {
        private Long m_transitionId;
        private Long m_sourceStateId;
        private Long m_destinationStateId;
        private Long m_statusId;
        private boolean m_isOwnerResponsible = false;
        private boolean m_isUserResponsible = false;
        private Collection<Long> m_assignedRoleIds;
            
        public Long getTransitionId() { return m_transitionId; }
        public void setTransitionId(Long transitionId) { m_transitionId = transitionId; }
        public Long getSourceStateId() { return m_sourceStateId; }
        public void setSourceStateId(Long sourceStateId) { m_sourceStateId = sourceStateId; }
        public Long getDestinationStateId() { return m_destinationStateId; }
        public void setDestinationStateId(Long destinationStateId) { m_destinationStateId = destinationStateId; }
        public Long getStatusId() { return m_statusId; }
        public void setStatusId(Long statusId) { m_statusId = statusId; }
        public boolean getIsOwnerResponsible() { return m_isOwnerResponsible; }
        public void setIsOwnerResponsible(boolean isOwnerResponsible) { m_isOwnerResponsible = isOwnerResponsible; }
        public boolean getIsUserResponsible() { return m_isUserResponsible; }
        public void setIsUserResponsible(boolean isUserResponsible) { m_isUserResponsible = isUserResponsible; }
        public Collection<Long> getAssignedRoleIds() { return m_assignedRoleIds; }
        public void setAssignedRoleIds(Collection<Long> assignedRoleIds) { m_assignedRoleIds = assignedRoleIds; }
    }
    
    private ArrayList<InternalTransition> m_transitionList;
    private Long m_taskStateMachineId;
    private int m_selectedValue = 0;
    private String m_cmd="";
    
    /** Creates a new instance of StateMachineActionForm */
    public StateMachineActionForm() {
        m_transitionList = new ArrayList();
    }
    
    @Override
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        for (InternalTransition transition: m_transitionList) {
            transition.setIsOwnerResponsible(false);
            transition.setIsUserResponsible(false);
            transition.setAssignedRoleIds(new ArrayList<Long>());
        }
    }
    
    public void setCmd(int index, String value) {
        m_selectedValue = index;
        m_cmd = value;
    }
    
    public String getCmd(int index) {
        return m_cmd;
    }
    
    public String getCmd() {
        return m_cmd;
    }
    
    public int getIndex() {
        return m_selectedValue;
    }
    
    public void setTransitionValues(Collection<Transition> transitions, Collection<IntegerString> allRolePairs) {
        Map<Integer, IntegerString> rolePairMap = new HashMap<Integer, IntegerString>();
        for (IntegerString pair: allRolePairs) {
            rolePairMap.put(pair.getInteger(), pair);
        }
        m_transitionList.clear();
        for (Transition transitionValue: transitions) {
            InternalTransition transition = new InternalTransition();
            transition.m_transitionId = transitionValue.getId();
            transition.m_sourceStateId = transitionValue.getSourceState().getId();
            transition.m_destinationStateId = transitionValue.getDestinationState().getId();
            transition.m_statusId = transitionValue.getStatus().getId();
            transition.m_isOwnerResponsible = transitionValue.isOwnerResponsible();
            transition.m_isUserResponsible = transitionValue.isUserResponsible();
            transition.m_assignedRoleIds = new ArrayList<Long>();
            for (Role role: transitionValue.getResponsibleRoles()) {
                transition.m_assignedRoleIds.add(role.getId());
            }
            m_transitionList.add(transition);
        }
    }
    
    public Collection<Transition> getTransitionValues(Collection<State> stateValues, Collection<Status> statusValues, Collection<Role> roleValues) {
        Map<Long,State> stateValueMap = new HashMap<Long,State>();
        for (State state: stateValues) {
            stateValueMap.put(state.getId(), state);
        }
        Map<Long,Status> statusValueMap = new HashMap<Long,Status>();
        for (Status status: statusValues) {
            statusValueMap.put(status.getId(), status);
        }
        Map<Long,Role> roleValueMap = new HashMap<Long,Role>();
        for (Role role: roleValues) {
            roleValueMap.put(role.getId(), role);
        }
        Collection<Transition> transitionValues = new ArrayList<Transition>();
        for (InternalTransition transition: m_transitionList) {
            Transition transitionValue = new Transition(transition.getIsOwnerResponsible(),
                    transition.getIsUserResponsible());
            if (transition.getTransitionId() != null)
                transitionValue.setId(transition.getTransitionId());
            transitionValue.setSourceState(stateValueMap.get(transition.getSourceStateId()));
            transitionValue.setDestinationState(stateValueMap.get(transition.getDestinationStateId()));
            transitionValue.setStatus(statusValueMap.get(transition.getStatusId()));
            Collection<Role> assignedRoleValues = new ArrayList<Role>();
            for (Long roleId: transition.getAssignedRoleIds()) {
                assignedRoleValues.add(roleValueMap.get(roleId));
            }
            transitionValue.setResponsibleRoles(assignedRoleValues);
            transitionValues.add(transitionValue);
        }
        return transitionValues;
    }
    
    public boolean processCmd(Collection allRolePairs) {
        if (m_cmd.equals("Add")) {
            InternalTransition transition = new InternalTransition();
            m_transitionList.add(transition);
            return true;
        }
        if (m_cmd.equals("Delete")) {
            m_transitionList.remove(m_selectedValue);
            return true;
        }
        return false;
    }
    
    public ArrayList getTransitionList() {
        return m_transitionList;
    }
    
    public void setTransitionList(ArrayList transitionList) {
        m_transitionList = transitionList;
    }
    
    public Long getTaskStateMachineId() {
        return m_taskStateMachineId;
    }
    
    public void setTaskStateMachineId(Long taskStateMachineId) {
        m_taskStateMachineId = taskStateMachineId;
    }
    
}
